Center - mean, median, mode
Spread - range, variance, standard deviation
Our last graphs
Practice Exercise 1: Recreating Our Last Histograms
To understand hypothesis testing need to understand standard normal distribution
Recall - sculpin in Toolik Lake
n = 208
mean = 51.69 mm
std dev = s = 12.02 mm
Weight distribution ~normal
Practice Exercise 2: Compare Fish Distributions from Different Lakes
Let’s look at what lakes are in our dataset:
[1] "E 01" "E 05" "NE 12" "NE 14" "S 06" "S 07" "Toolik"
Now, select two lakes and create a comparison of their fish length distributions using facet_grid():
You want to know things about this population like
Standard Normal Distribution
~68% of the curve area within +/- 1 σ of the mean,
~95% within +/- 2 σ of the mean,
~99.7% within +/- 3 σ of the mean
*remember σ = standard deviation
Areas under curve of Standard Normal Distribution
Done by converting original data points to z-scores
Practice Exercise 3: Calculate Z-Scores
Let’s practice calculating z-scores for fish lengths. Calculate the z-scores for:
Now use your calculated z-scores to determine roughly where these fish fall in the overall distribution: - What percentage of fish are smaller than the 25 mm fish? - What percentage of fish are larger than the 60 mm fish?
Thus:
Area under curve (probability) of standard normal distribution is known relative to z-values
Knowing z-value, can figure out corresponding area under the curve
What is the area under curve < 0?
Here is z-score table for right side or positive values of the z distribution (z > 0)
Numbers give area under the curve to left of a particular z-score
say 60 mm as a z score of 0.6916667
Area under curve (probability) of standard normal distribution is known relative to z-values
Knowing z-value, can figure out corresponding area under the curve
What is the area under curve < 0?
area of the curve is contained to the left of z = 1.22
Practice Exercise 4: Working with Probabilities
Using the z-table or the built-in R function pnorm(), answer these questions:
# Convert 65 mm to a z-score
z_65mm <- (65 - toolik_result$mean) / toolik_result$sd
# Find the probability of a fish being larger than 65 mm
prob_larger_than_65mm <- 1 - pnorm(z_65mm)
prob_larger_than_65mm * 100 # Convert to percentage[1] 13.4254
# Find the z-scores for the 5th and 95th percentiles
z_5 <- qnorm(0.05)
z_95 <- qnorm(0.95)
# Convert these z-scores back to fish lengths
length_5 <- toolik_result$mean + z_5 * toolik_result$sd
length_95 <- toolik_result$mean + z_95 * toolik_result$sd
c(length_5, length_95) # The middle 90% of fish lengths[1] 31.91600 71.47343
What is the area of the curve is contained between of z = 0 and z=1.5?
What is the area of the curve is contained between of z = 0 and z=1.5?
To calculate this from a standard normal table:
To find the area under the standard normal curve between 0 and 1.5 using this table:
What is the area of the curve is contained to the left of z = -1?
What is the area of the curve is contained to the left of z = -1?
need to use the symmetry property of the standard normal distribution:
P(Z ≤ -1.0) = 1 - P(Z ≤ 1.0) = 1 - 0.8413 = 0.1587
Therefore, 15.87% of area falls to the left of z = -1.0
Take random samples from fish population:
3 random samples (each n=20) from population:
Notice the sample statistics and distributions
Practice Exercise 5: Sampling Distributions
Let’s explore how sample size affects our estimates by taking samples of different sizes:
# Set seed for reproducibility
set.seed(456)
# Create samples of different sizes
small_sample <- toolik_df %>% sample_n(10)
medium_sample <- toolik_df %>% sample_n(30)
large_sample <- toolik_df %>% sample_n(100)
# Calculate mean and standard error for each sample
small_mean <- mean(small_sample$total_length_mm, na.rm = TRUE)
small_se <- sd(small_sample$total_length_mm, na.rm = TRUE) / sqrt(10)
medium_mean <- mean(medium_sample$total_length_mm, na.rm = TRUE)
medium_se <- sd(medium_sample$total_length_mm, na.rm = TRUE) / sqrt(30)
large_mean <- mean(large_sample$total_length_mm, na.rm = TRUE)
large_se <- sd(large_sample$total_length_mm, na.rm = TRUE) / sqrt(100)
# Create a data frame with the results
results <- data.frame(
Sample_Size = c(10, 30, 100),
Mean = c(small_mean, medium_mean, large_mean),
SE = c(small_se, medium_se, large_se)
)
# Display the results
results Sample_Size Mean SE
1 10 58.50000 2.260531
2 30 51.27083 1.656559
3 100 52.22973 1.062795
What do you observe about the standard error as sample size increases? Why does this happen?
Every sample gives slightly different estimate of µ
Given above
can estimate the standard deviation of sample means
“Standard error of sample mean”
How good is your estimate of population mean? (based on the sample collected)
quantifies how much the sample means are expected to vary from samples
gives an estimate of the error associated with using \(\bar{y}\) to estimate \(\mu\)…
\(\sigma_{\bar{y}} = \frac{\sigma}{\sqrt{n}}\)
but rarely know σ, so use s \(s_{\bar{y}} = \frac{s}{\sqrt{n}}\) Where: \(s_{\bar{y}}\) = sample standard error of mean s = sample standard deviation n = sample size
Notice: - \(s_{\bar{y}}\) depends on - sample s (standard deviation) - sample n - (\(s_{\bar{y}} = \frac{s}{\sqrt{n}}\))
How and why? - Decreases with sample n - number - increases with sample s - standard deviation
Every sample gives slightly different estimate of µ (population mean)
Want to know how accurate our estimate of µ is from a sample
Do this by calculating confidence interval:
Practice Exercise 6: Calculating Confidence Intervals
Let’s calculate 95% confidence intervals for the mean fish length in Toolik Lake:
# Calculate the standard error for Toolik Lake
toolik_se <- toolik_result$sd / sqrt(toolik_result$count)
# Calculate the 95% confidence interval using the normal distribution
# (since our sample size is large)
toolik_ci_lower <- toolik_result$mean - 1.96 * toolik_se
toolik_ci_upper <- toolik_result$mean + 1.96 * toolik_se
# Print the results
cat("Mean fish length in Toolik Lake:", round(toolik_result$mean, 1), "mm\n")Mean fish length in Toolik Lake: 51.7 mm
95% Confidence Interval: 50.1 to 53.3 mm
Now choose another lake and calculate its 95% confidence interval:
# Choose another lake (e.g., "E 01")
my_lake <- "E 01" # You can change this to any lake in the dataset
# Filter data for your chosen lake
my_lake_data <- sculpin_df %>% filter(lake == my_lake)
# Calculate mean, standard deviation, and standard error
my_lake_stats <- my_lake_data %>%
summarize(
mean = mean(total_length_mm, na.rm = TRUE),
sd = sd(total_length_mm, na.rm = TRUE),
n = sum(!is.na(total_length_mm)),
se = sd / sqrt(n)
)
# Calculate 95% confidence interval
my_lake_ci_lower <- my_lake_stats$mean - 1.96 * my_lake_stats$se
my_lake_ci_upper <- my_lake_stats$mean + 1.96 * my_lake_stats$se
# Print results
cat("Mean fish length in", my_lake, ":", round(my_lake_stats$mean, 1), "mm\n")Mean fish length in E 01 : 58.2 mm
cat("95% Confidence Interval:", round(my_lake_ci_lower, 1), "to", round(my_lake_ci_upper, 1), "mm\n")95% Confidence Interval: 54.8 to 61.6 mm
Do the confidence intervals for these two lakes overlap? What does this suggest about the difference between the fish populations?
Often calculate 95% CIs
asdfasfasd
Formula for confidence interval
Where:
Formula for confidence interval
\(\text{95% CI} = \bar{y} \pm z \cdot \frac{\sigma}{\sqrt{n}}\)
95% of probability of SND is bw z= -1.96 and z=1.96
So for:
In the more typical case DON’T know the population σ - estimate it from the sample s When don’t know the population σ - and when sample size is < ~30) - can’t use the standard normal (z) distribution
Instead, we use Student’s t distribution
Student’s t distribution similar to SND
At df = ~30 - t distribution becomes close to z distribution
Practice Exercise 7: Using the t-Distribution
When working with small samples or when the population standard deviation is unknown, we use the t-distribution. Let’s take a small sample and calculate a confidence interval using the t-distribution:
# Set seed for reproducibility
set.seed(789)
# Take a small sample of 15 fish from Toolik Lake
small_sample <- toolik_df %>% sample_n(15)
# Calculate sample statistics
small_mean <- mean(small_sample$total_length_mm, na.rm = TRUE)
small_sd <- sd(small_sample$total_length_mm, na.rm = TRUE)
small_n <- 15
small_se <- small_sd / sqrt(small_n)
# Calculate degrees of freedom
df <- small_n - 1
# Find the critical t-value for 95% confidence interval
t_critical <- qt(0.975, df) # 0.975 for a two-tailed 95% CI
# Calculate the confidence interval
small_ci_lower <- small_mean - t_critical * small_se
small_ci_upper <- small_mean + t_critical * small_se
# Print the results
cat("Small sample (n=15) mean:", round(small_mean, 1), "mm\n")Small sample (n=15) mean: 57.3 mm
t-critical value (df=14): 2.145
cat("95% CI using t-distribution:", round(small_ci_lower, 1), "to", round(small_ci_upper, 1), "mm\n")95% CI using t-distribution: 52.7 to 61.9 mm
# For comparison, calculate the CI using the normal distribution (z)
z_critical <- 1.96
z_ci_lower <- small_mean - z_critical * small_se
z_ci_upper <- small_mean + z_critical * small_se
cat("95% CI using normal distribution:", round(z_ci_lower, 1), "to", round(z_ci_upper, 1), "mm\n")95% CI using normal distribution: 53.1 to 61.5 mm
Which confidence interval is wider? Why is this the case?
To calculate CI for sample from “unknown” population:
\(\text{CI} = \bar{y} \pm t \cdot \frac{s}{\sqrt{n}}\)
Where:
Here is a t-table
One-tailed questions: area of distribution left or (right) of a certain value
Two-tailed questions refer to area between certain values
Let’s calculate CIs again:
Use two-sided test
So:
Practice Exercise 8: One-Sample t-Test
Let’s perform a one-sample t-test to determine if the mean fish length in Toolik Lake differs from 50 mm:
# Perform a one-sample t-test
t_test_result <- t.test(toolik_df$total_length_mm, mu = 50)
# View the test results
t_test_result
One Sample t-test
data: toolik_df$total_length_mm
t = 2.0326, df = 207, p-value = 0.04337
alternative hypothesis: true mean is not equal to 50
95 percent confidence interval:
50.05097 53.33845
sample estimates:
mean of x
51.69471
Interpret this test result by answering these questions: 1. What was the null hypothesis? 2. What was the alternative hypothesis? 3. What does the p-value tell us? 4. Should we reject or fail to reject the null hypothesis at α = 0.05? 5. What is the practical interpretation of this result for fish biologists?
For example
How would you assess this question using what we learned?
Practice Exercise 9: Two-Sample t-Test
Let’s compare fish lengths between two lakes to see if they differ:
# Choose two lakes to compare
lake1 <- "Toolik"
lake2 <- "E 01" # Change this to another lake if you prefer
# Filter data for the two lakes
lake1_data <- sculpin_df %>%
filter(lake == lake1) %>%
pull(total_length_mm)
lake2_data <- sculpin_df %>%
filter(lake == lake2) %>%
pull(total_length_mm)
# Perform a two-sample t-test
lakes_ttest <- t.test(lake1_data, lake2_data)
# View the results
lakes_ttest
Welch Two Sample t-test
data: lake1_data and lake2_data
t = -3.4051, df = 116.36, p-value = 0.0009082
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-10.313036 -2.727921
sample estimates:
mean of x mean of y
51.69471 58.21519
Now create a boxplot to visualize the difference in fish lengths between these lakes:
# Create a boxplot comparing the two lakes
sculpin_df %>%
filter(lake %in% c(lake1, lake2)) %>%
ggplot(aes(x = lake, y = total_length_mm, fill = lake)) +
geom_boxplot() +
labs(
title = paste("Comparison of Fish Lengths in", lake1, "and", lake2),
x = "Lake",
y = "Length (mm)"
) +
theme_minimal()Warning: Removed 268 rows containing non-finite outside the scale range
(`stat_boxplot()`).
Based on the t-test results and the boxplot, what can you conclude about the fish populations in these two lakes?
Let’s calculate the 95% CI for population X
Use two-sided test
95% CI Sample X: = 54 ± 1.984 * (10.9/(132^0.5)) = 1.882267 The 95% CI is between 52.12 and 55.88
Notice: the 95% confidence interval contains 51.7
Major goal of statistics:
inferences about populations from samples assign degree of confidence to inferences
Statistical H-testing:
formalized approach to inference
Relies on specifying null hypothesis (Ho) and alternate hypothesis (Ha)
Practice Exercise 10: Formulating Hypotheses
For the following scenarios, write out the null and alternative hypotheses:
Testing if the mean fish length in Lake S 06 is greater than 50 mm.
Testing if there is a difference in mean fish lengths between lakes Toolik and S 06.
Testing if lake E 01 has a higher variance in fish lengths compared to Lake Toolik.
For each scenario, remember that the null hypothesis typically represents “no effect” or “no difference”, while the alternative hypothesis represents what you are trying to demonstrate.
Which p-value suggests Ho likely false?
At what point reject Ho?
p < 0.05 conventional “significance threshold” (α)
p < 0.05 means:
α is the rate at which we will reject a true null hypothesis (Type I error rate)
Lowering α will lower likelihood of incorrectly rejecting a true null hypothesis (e.g., 0.01, 0.001)
Both hypotheses and α are specified BEFORE collection of data and analysis
Traditionally α=0.05 is used as a cut off for rejecting null hypothesis
Nothing magical about 0.0 - actual p-values need to be reported.
| p-value range | Interpretation |
|---|---|
| P > 0.10 | No evidence against Ho - data appear consistent with Ho |
| 0.05 < P < 0.10 | Weak evidence against the Ho in favor of Ha |
| 0.01 < P < 0.05 | Moderate evidence against Ho in favor of Ha |
| 0.001 < P < 0.01 | Strong evidence against Ho in favor of Ha |
| P < 0.001 | Very strong evidence against Ho in favor of Ha |
Fisher:
p-value as informal measure of discrepancy betwen data and Ho
“If p is between 0.1 and 0.9 there is certainly no reason to suspect the hypothesis tested. If it is below 0.02 it is strongly indicated that the hypothesis fails to account for the whole of the facts. We shall not often be astray if we draw a conventional line at .05 …”
s
General procedure for H testing:
General procedure for H testing:
Final Exercise: Comprehensive Analysis
You’ve learned about standard normal distributions, z-scores, standard error, confidence intervals, and hypothesis testing. Now, put it all together with a comprehensive analysis of fish lengths from multiple lakes.
# Choose 3 lakes for comparison
lakes_to_compare <- c("Toolik", "E 01", "S 06")
# Filter data for these lakes
comparison_data <- sculpin_df %>%
filter(lake %in% lakes_to_compare) %>%
filter(!is.na(total_length_mm))
# 1. Calculate summary statistics for each lake
lake_stats <- comparison_data %>%
group_by(lake) %>%
summarize(
mean_length = mean(total_length_mm, na.rm = TRUE),
sd_length = sd(total_length_mm, na.rm = TRUE),
n = sum(!is.na(total_length_mm)),
se_length = sd_length / sqrt(n),
ci_lower = mean_length - qt(0.975, n-1) * se_length,
ci_upper = mean_length + qt(0.975, n-1) * se_length
)
# Display the summary statistics
lake_stats# A tibble: 3 × 7
lake mean_length sd_length n se_length ci_lower ci_upper
<chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 E 01 58.2 15.3 79 1.72 54.8 61.6
2 S 06 54.0 10.9 132 0.949 52.1 55.9
3 Toolik 51.7 12.0 208 0.834 50.1 53.3
# 2. Create boxplots to visualize the distributions
comparison_data %>%
ggplot(aes(x = lake, y = total_length_mm, fill = lake)) +
geom_boxplot() +
labs(
title = "Fish Length Distributions Across Lakes",
x = "Lake",
y = "Length (mm)"
) +
theme_minimal()# 3. Perform ANOVA to test for differences among the lakes
lake_anova <- aov(total_length_mm ~ lake, data = comparison_data)
summary(lake_anova) Df Sum Sq Mean Sq F value Pr(>F)
lake 2 2459 1229.6 8.018 0.000383 ***
Residuals 416 63800 153.4
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 4. If ANOVA is significant, perform post-hoc tests
if(summary(lake_anova)[[1]]["Pr(>F)"][1,] < 0.05) {
tukey_results <- TukeyHSD(lake_anova)
print(tukey_results)
} Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = total_length_mm ~ lake, data = comparison_data)
$lake
diff lwr upr p adj
S 06-E 01 -4.200038 -8.343516 -0.05656054 0.0461139
Toolik-E 01 -6.520478 -10.370118 -2.67083905 0.0002356
Toolik-S 06 -2.320440 -5.561933 0.92105332 0.2126069
Based on this analysis, write a short summary of your findings: 1. Are there significant differences in fish lengths between the lakes? 2. Which lakes have the largest and smallest fish on average? 3. How do the confidence intervals compare across lakes? 4. What might explain these differences in fish lengths between lakes?
text
place to put figure with this text